﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MySQLManagerData : System.Web.UI.Page
{
    private int perPage = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData(0*perPage,perPage);
            LBtnNext.PostBackUrl = "?page=2";
            if (Request.QueryString["action"] != null)
            {
                long id = Convert.ToInt64(Request.QueryString["id"]);
                switch (Request.QueryString["action"])
                {
                    case "edit":
                        update(id);
                        break;
                    case "delete":
                        delete(id);
                        break;
                }
            }
        }
    }

    private void delete(long id)
    {
        InfoDao.Delete(id);
    }

    private void update(long id)
    {
       
        Info data = InfoDao.Find(id);
        TbUser.Text = data.AdminName;
        TbPassword.Text = data.Password;
        HfId.Value = data.Id.ToString();
        SaveInfo();
    }

    protected void LBtn_Click(object sender, EventArgs e)
    {
        int page = Convert.ToInt32(Request.QueryString["page"]);
        LoadData((page - 1) * perPage, perPage);
       LBtnNext.PostBackUrl = "?page=" + (page + 1);
    }
    private void LoadData(int start, int num)
    {

        try
        {
            Literal1.Text = "<table>\n";
            Literal1.Text += "<tr>\n";
            Literal1.Text += "<th>序号</th>\n";
            Literal1.Text += "<th>用户名</th>\n";
            Literal1.Text += "<th>密码</th>\n";
            Literal1.Text += "<th>&nbsp;</th>\n";
            Literal1.Text += "<tr>\n";
            foreach (var data in InfoDao.FindAll(start, num))
            {
                Literal1.Text += "<tr>\n";
                Literal1.Text += "<td>" + data.Id + "</td>\n";
                Literal1.Text += "<td>" + data.AdminName + "</td>\n";
                Literal1.Text += "<td>" + data.Password + "</td>\n";
                Literal1.Text += "<td><a href=\"?action=edit&id="+data.Id+ "\">编辑</a><a href=\"?action=delete&id=" + data.Id + "\">删除</a></td>\n";
                Literal1.Text += "<tr>\n";
            }
            Literal1.Text += "</table>\n";
        }
        catch (Exception)
        {
            Literal1.Text = "数据加载异常!";
        }
    }

    protected void BtnInfoSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (HfId.Value.Equals("-1"))//新增界面
            {
                AddInfo();
            }
            else
            {//修改记录
                SaveInfo();
            }
        }
        catch
        {
            Literal1.Text = "数据处理异常";
        }
    }

    private void SaveInfo()
    {
        Info data = InfoDao.Find(Convert.ToInt64(HfId.Value));
        data.AdminName = TbUser.Text.Trim();
        data.Password = TbPassword.Text.Trim();
        InfoDao.Update(data);
        
    }

    private void AddInfo()
    {
        Info data = new Info();
        data.AdminName = TbUser.Text.Trim();
        data.Password = TbPassword.Text.Trim();
        long insertId = InfoDao.Insert(data);
        Debug.WriteLine("插入数据返回Id:" + insertId);
    }
}